import {
  getDescPageMetaList,
  getKeywordsPageMetaList,
  getTitlePageMetaList
} from "../../../stands/PageMetaForm/js/pageMetaFuns";
import pageMetaData from "../../../stands/PageMetaForm/js/pageMetaData";
import ApiBase from "../../../../api/moudles/ApiBase";



const blogDic = {
  unPublishedState: 0,
  publishedState: 1,
  previewedPublishState: 1,
  modifiedPublishState: 3
}


/**
 * 查询blog列表
 * @param self
 * @param tiyData
 */

export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiBlog.blogList({
  }).then(function (response){
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.blogList.splice(0, tiyData.tData.blogList.length);
      let blogListResult = response.result;
      blogListResult.forEach(
        function (item) {
          tiyData.tData.blogList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

export const listDirectory = (self, tiyData) => {
  self.$Spin.show();
  self.$apiBlog.queryDirectoryList({
  }).then(function (response){
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.directoryList.splice(0);
      tiyData.tData.directoryList.push(...response.result);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  self.$refs.paramBlogModel.resetFields();
  tiyData.tParam.blog.id='';
  tiyData.tParam.blog.title='';
  tiyData.tParam.blog.subtitle='';
  tiyData.tParam.blog.content='';
  tiyData.tParam.blog.linkName='';
  tiyData.tParam.blog.linkUrl='';
  tiyData.tParam.blog.sequence=0;
  tiyData.tParam.blog.directoryId="";
  tiyData.tParam.pageMetaInfos.splice(0,tiyData.tParam.pageMetaInfos.length);

};

export const initModifyBlog = (self, tiyData,result) => {
  self.$refs.paramBlogModel.resetFields();
  tiyData.tParam.blog.id=result.id;
  tiyData.tParam.blog.title=result.title;
  tiyData.tParam.blog.subtitle=result.subtitle;
  tiyData.tParam.blog.content=result.content;
  tiyData.tParam.blog.linkUrl=result.linkUrl;
  tiyData.tParam.blog.linkName=result.linkName;
  tiyData.tParam.blog.sequence=result.sequence;
  tiyData.tParam.blog.directoryId=result.directoryId;
  tiyData.tParam.pageMetaInfos.splice(0,tiyData.tParam.pageMetaInfos.length);
  tiyData.tParam.pageMetaInfos.push(...result.pageMetaInfos);

};

export const initDirectory = (self, tiyData) => {
  self.$refs.directory.resetFields();
  tiyData.tParam.directory.id='';
  tiyData.tParam.directory.name='';
  tiyData.tParam.directory.sequence=0;
  tiyData.tParam.directory.state=0;

};

export const initModifyDirectory = (self, tiyData,result) => {
  self.$refs.directory.resetFields();
  tiyData.tParam.directory.id=result.id;
  tiyData.tParam.directory.name=result.name;
  tiyData.tParam.directory.sequence=result.sequence;
  tiyData.tParam.directory.state=result.state;

};


export const save = (self, tiyData) => {
  self.$refs["paramBlogModel"].validate((valid) => {
    if (valid) {
      self.$Spin.show();
      tiyData.tParam.blog.linkUrl = tiyData.tParam.blog.linkName+".html";
      self.$apiBlog.saveBlog(
        {'blog':tiyData.tParam.blog,
          pageMetaInfos:tiyData.tParam.pageMetaInfos
        }).then(function (response){
        self.$Spin.hide();
        if (response.success) {
          self.list();
          if(!tiyData.tStatus.modalView.isModify){
            tiyData.tStatus.modalView.isModify=true;
            initModifyBlog(self,tiyData,response.result);
          }else{
            self.modalView.isShow= false;
          }
        } else {
          self.$Message.error(response.errorMessage);
        }
      });


    }
  })

};

/**
 * 保存SEO信息(新增blog和blog一起保存，需改可以单独保存）
 * @param self
 * @param tiyData
 */
export const saveBlogSEO = (self, tiyData) => {
  let pageMetaInfos = [];
  pageMetaInfos.push(...getTitlePageMetaList(pageMetaData.pageMetaDic.blogType));
  pageMetaInfos.push(...getDescPageMetaList(pageMetaData.pageMetaDic.blogType));
  pageMetaInfos.push(...getKeywordsPageMetaList(pageMetaData.pageMetaDic.blogType));

  tiyData.tParam.pageMetaInfos.splice(0,tiyData.tParam.pageMetaInfos.length);
  tiyData.tParam.pageMetaInfos=pageMetaInfos;
  if(tiyData.tStatus.modalView.isModify){
    let params = {
      target:tiyData.tParam.blog.id,
      pageMetaInfos: pageMetaInfos,
      type:6
    }
    self.$Spin.show();
    self.$apiOther.savePageMetaInfos(params).then(response => {
      self.$Spin.hide();
      if(response.success) {
        self.$Message.success("保存成功！");
        tiyData.tStatus.modalModifySEO.isShow=false;

      }else{
        self.$Message.error(response.errorMessage);
      }

    })
  }else{
    tiyData.tStatus.modalModifySEO.isShow=false;
  }

}


export const edit = (self, tiyData,id) => {
  self.$Spin.show();
  self.$apiBlog.queryById(
    {'blogId':id
    }).then(function (response){
    self.$Spin.hide();
    if (response.success) {
      tiyData.tStatus.modalView.isShow=true;
      tiyData.tStatus.modalView.isModify=true;
      initModifyBlog(self,tiyData,response.result);
      self.$Spin.hide();
    } else {
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }
  });
}

export const saveDirectory = (self, tiyData) => {
  self.$refs["directory"].validate((valid) => {
    if (valid) {
      self.$Spin.show();
      self.$apiBlog.saveDirectory(
        {'directory':tiyData.tParam.directory,
        }).then(function (response){
        self.$Spin.hide();
        if (response.success) {
          tiyData.tStatus.modalDirectory.isShow = false;
          self.list();
        } else {
          self.$Message.error(response.errorMessage);
        }
      });
    }
  })

};

export const editDirectory = (self, tiyData,id) => {
  self.$Spin.show();
  self.$apiBlog.queryDirectoryById(
    {'directoryId':id
    }).then(function (response){
    self.$Spin.hide();
    if (response.success) {
      tiyData.tStatus.modalDirectory.isShow = true
      tiyData.tStatus.modalDirectory.isModify=true;
      initModifyDirectory(self,tiyData,response.result);
      self.$Spin.hide();
    } else {
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }
  });

};

export const deleteDirectory = (self, tiyData,id) => {

  self.$Modal.confirm({
    title: "确认",
    content: "确定删除该目录吗？",
    okText: "删除",
    cancelText: "取消",
    onOk() {
      self.$Spin.show();
      self.$apiBlog.deleteDirectory(
        {'directoryId':id
        }).then(function (response){
        self.$Spin.hide();
        if (response.success) {
          self.$Spin.hide();
        } else {
          self.$Spin.hide();
          self.$Message.error(response.errorMessage);
        };
        self.list();
      });
    }
  });


};

export const deleteBlog = (self, tiyData,id) => {

  self.$Modal.confirm({
    title: "确认",
    content: "确定删除Blog吗？",
    okText: "删除",
    cancelText: "取消",
    onOk() {
      let params = {id: id};
      self.$Spin.show();
      self.$apiBlog.deleteBlog(
        {'blogId':id
        }).then(function (response){
        self.$Spin.hide();
        if (response.success) {

          self.$Spin.hide();
        } else {
          self.$Spin.hide();
          self.$Message.error(response.errorMessage);
        }
        self.list();
      });
    }
  });



};

export const beforePreviewBlog = (self,tiyData, blog) => {
  if(!blog.linkUrl){
    self.$Message.error("Blog没有固定网址，不能预览！");
    return;
  }
  self.$Spin.show();
  previewBlog(self,tiyData,blog);
}

export const previewBlog = (self,tiyData, blog) => {
  self.$Spin.show();
  let previewUrl = self.$store.getters.webDomain+"blog/prepareBlog.do?code=shidai_912&blogId="+blog.id;
  self.$apiBlog.previewBlog(previewUrl,{}).then(response => {
    if(response.success) {
      self.$Message.success("生成页面成功！");
      self.list();
      window.open(self.$store.getters.webDomain+'tmp/'+response.result, "_blank")
    }else{
      self.$Message.success(response.errorMessage);
    }
    self.$Spin.hide();

  });
}

export const beforePublishPage = (self,tiyData,blog) => {

  if(!blog.linkUrl){
    self.$Message.error("Blog没有固定网址，不能预览！");
    return;
  }
  self.$Spin.show();
  publishPage(self,tiyData, blog);
}

export const publishPage = (self,tiyData,blog) => {
  let shortLink= encodeURIComponent(blog.linkUrl);
  self.$Spin.show();
  let publishPageUrl = self.$store.getters.webDomain+"blog/publishBlog.do?code=shidai_912&fileName="+shortLink+"&blogId="+blog.id;
  ApiBase.send(publishPageUrl,{}).then(response => {
      if(response.success) {
        self.$Message.success("页面发布成功！");
      }else{
        let errorMsg = "页面发布失败！";
        if(response.errorMessage){
          errorMsg += response.errorMessage;
        }
        self.$Message.error(errorMsg);
      }
      self.$Spin.hide();
      self.list();
    }

  )
}
